$(function(){
	var curPage = 1;
	var pageSize = 9;
	
	function getParams(orderby){
		return {
			curPage:curPage,
			pageSize:pageSize,
			locationUuid:$("#locations .selected").attr("value"),
			styleUuid:$("#styles .selected").attr("value"),
			minArea:$("#areas .selected").attr("min"),
			maxArea:$("#areas .selected").attr("max"),
			minPrice:$("#prices .selected").attr("min"),
			maxPrice:$("#prices .selected").attr("max"),
			productTypeUuid:$("#productTypes .selected").attr("value"),
			openUuid:$("#opens .selected").attr("value"),
			imageLocation:"4",
			orderby:orderby
		}
	}
	//按综合排序
	$("#productNameFlag").bind("click",function(){
		var orderby = "asc";
		//如果是升序
		if($("#productNameFlag b").attr("class").indexOf("sorted-asc-b")!=-1){
			orderby = "desc";
		}
		$.ajax({
			url:"/"+Udwin.appName+"/product/search",
			data:getParams("product_name "+orderby),
			method:"POST",
			success:function(data){
				$(".sorted").removeClass("sorted").addClass("un-sorted");
				$("#productNameFlag").removeClass("un-sorted").addClass("sorted");
				if(orderby=="asc"){
					$("#productNameFlag b").removeClass("sorted-desc-b").addClass("sorted-asc-b");
				}else{
					$("#productNameFlag b").removeClass("sorted-asc-b").addClass("sorted-desc-b");
				}
				$("#products").empty().html(data);
				//跳转到第一页
				$(".ui-page-num a").removeClass("ui-page-cur").eq(1).addClass("ui-page-cur");
				$("#jumpto").val("1");
			}
		});
	})
	//按销量排序
	$("#salesVolumeFlag").bind("click",function(){
		var orderby = "asc";
		//如果是升序
		if($("#salesVolumeFlag b").attr("class").indexOf("sorted-asc-b")!=-1){
			orderby = "desc";
		}
		
		$.ajax({
			url:"/"+Udwin.appName+"/product/search",
			data:getParams("sales_volume "+orderby),
			method:"POST",
			success:function(data){
				$(".sorted").removeClass("sorted").addClass("un-sorted");
				$("#salesVolumeFlag").removeClass("un-sorted").addClass("sorted");
				if(orderby=="asc"){
					$("#salesVolumeFlag b").removeClass("sorted-desc-b").addClass("sorted-asc-b");
				}else{
					$("#salesVolumeFlag b").removeClass("sorted-asc-b").addClass("sorted-desc-b");
				}
				$("#products").empty().html(data);
				//跳转到第一页
				$(".ui-page-num a").removeClass("ui-page-cur").eq(1).addClass("ui-page-cur");
				$("#jumpto").val("1");
			}
		});
	})
	//按价格排序
	$("#priceFlag").bind("click",function(){
		var orderby = "asc";
		//如果是升序
		if($("#priceFlag b").attr("class").indexOf("sorted-asc-b")!=-1){
			orderby = "desc";
		}
		
		$.ajax({
			url:"/"+Udwin.appName+"/product/search",
			data:getParams("price "+orderby),
			method:"POST",
			success:function(data){
				$(".sorted").removeClass("sorted").addClass("un-sorted");
				$("#priceFlag").removeClass("un-sorted").addClass("sorted");
				if(orderby=="asc"){
					$("#priceFlag b").removeClass("sorted-desc-b").addClass("sorted-asc-b");
				}else{
					$("#priceFlag b").removeClass("sorted-asc-b").addClass("sorted-desc-b");
				}
				$("#products").empty().html(data);
				//跳转到第一页
				$(".ui-page-num a").removeClass("ui-page-cur").eq(1).addClass("ui-page-cur");
				$("#jumpto").val("1");
			}
		});
	})
	//按最新排序
	$("#newsFlag").bind("click",function(){
		var orderby = "asc";
		//如果是升序
		if($("#newsFlag b").attr("class").indexOf("sorted-asc-b")!=-1){
			orderby = "desc";
		}
		
		$.ajax({
			url:"/"+Udwin.appName+"/product/search",
			data:getParams("up.update_time "+orderby),
			method:"POST",
			success:function(data){
				$(".sorted").removeClass("sorted").addClass("un-sorted");
				$("#newsFlag").removeClass("un-sorted").addClass("sorted");
				if(orderby=="asc"){
					$("#newsFlag b").removeClass("sorted-desc-b").addClass("sorted-asc-b");
				}else{
					$("#newsFlag b").removeClass("sorted-asc-b").addClass("sorted-desc-b");
				}
				$("#products").empty().html(data);
				//跳转到第一页
				$(".ui-page-num a").removeClass("ui-page-cur").eq(1).addClass("ui-page-cur");
				$("#jumpto").val("1");
			}
		});
	})
	
	//按场景搜索
	$("#locations li").bind("click",function(){
		$("#locations .selected").removeClass("selected");
		var orderby = $(".sorted").attr("field");
		if($(".sorted b").attr("class").indexOf("asc")!=-1){
			orderby = orderby +" " + "asc";
		}else{
			orderby = orderby +" " + "desc";
		}
		console.log(orderby)
		$(this).addClass("selected");
		$.ajax({
			url:"/"+Udwin.appName+"/product/search",
			data:getParams(orderby),
			method:"POST",
			success:function(data){
				$("#products").empty().html(data);
				$("#countTitle").empty().html($("#searchProductSize").val());
			}
		});
		
		$.ajax({
			url:"/"+Udwin.appName+"/product/pages",
			data:getParams(orderby),
			method:"POST",
			success:function(data){
				$(".ui-page").empty().html(data);
			}
		});
	})
	
	//按风格搜索
	$("#styles li").bind("click",function(){
		$("#styles .selected").removeClass("selected");
		$(this).addClass("selected");
		var orderby = $(".sorted").attr("field");
		if($(".sorted b").attr("class").indexOf("asc")!=-1){
			orderby = orderby +" " + "asc";
		}else{
			orderby = orderby +" " + "desc";
		}
		console.log(orderby)
		$.ajax({
			url:"/"+Udwin.appName+"/product/search",
			data:getParams(orderby),
			method:"POST",
			success:function(data){
				$("#products").empty().html(data);
				$("#countTitle").empty().html($("#searchProductSize").val());
			}
		});
		
		$.ajax({
			url:"/"+Udwin.appName+"/product/pages",
			data:getParams(orderby),
			method:"POST",
			success:function(data){
				$(".ui-page").empty().html(data);
			}
		});
	})
	
	//按风格搜索
	$("#areas li").bind("click",function(){
		$("#areas .selected").removeClass("selected");
		$(this).addClass("selected");
		var orderby = $(".sorted").attr("field");
		if($(".sorted b").attr("class").indexOf("asc")!=-1){
			orderby = orderby +" " + "asc";
		}else{
			orderby = orderby +" " + "desc";
		}
		console.log(orderby)
		$.ajax({
			url:"/"+Udwin.appName+"/product/search",
			data:getParams(orderby),
			method:"POST",
			success:function(data){
				$("#products").empty().html(data);
				$("#countTitle").empty().html($("#searchProductSize").val());
			}
		});
		
		$.ajax({
			url:"/"+Udwin.appName+"/product/pages",
			data:getParams(orderby),
			method:"POST",
			success:function(data){
				$(".ui-page").empty().html(data);
			}
		});
	})
	//价格范围搜索
	$("#prices li").bind("click",function(){
		$("#prices .selected").removeClass("selected");
		$(this).addClass("selected");
		var orderby = $(".sorted").attr("field");
		if($(".sorted b").attr("class").indexOf("asc")!=-1){
			orderby = orderby +" " + "asc";
		}else{
			orderby = orderby +" " + "desc";
		}
		console.log(orderby)
		$.ajax({
			url:"/"+Udwin.appName+"/product/search",
			data:getParams(orderby),
			method:"POST",
			success:function(data){
				$("#products").empty().html(data);
				$("#countTitle").empty().html($("#searchProductSize").val());
			}
		});
		
		$.ajax({
			url:"/"+Udwin.appName+"/product/pages",
			data:getParams(orderby),
			method:"POST",
			success:function(data){
				$(".ui-page").empty().html(data);
			}
		});
	})
	//用途搜索
	$("#productTypes li").bind("click",function(){
		$("#productTypes .selected").removeClass("selected");
		$(this).addClass("selected");
		var orderby = $(".sorted").attr("field");
		if($(".sorted b").attr("class").indexOf("asc")!=-1){
			orderby = orderby +" " + "asc";
		}else{
			orderby = orderby +" " + "desc";
		}
		console.log(orderby)
		$.ajax({
			url:"/"+Udwin.appName+"/product/search",
			data:getParams(orderby),
			method:"POST",
			success:function(data){
				$("#products").empty().html(data);
				$("#countTitle").empty().html($("#searchProductSize").val());
			}
		});
		
		$.ajax({
			url:"/"+Udwin.appName+"/product/pages",
			data:getParams(orderby),
			method:"POST",
			success:function(data){
				$(".ui-page").empty().html(data);
			}
		});
	})
	//开启方式搜索
	$("#opens li").bind("click",function(){
		$("#opens .selected").removeClass("selected");
		$(this).addClass("selected");
		var orderby = $(".sorted").attr("field");
		if($(".sorted b").attr("class").indexOf("asc")!=-1){
			orderby = orderby +" " + "asc";
		}else{
			orderby = orderby +" " + "desc";
		}
		console.log(orderby)
		$.ajax({
			url:"/"+Udwin.appName+"/product/search",
			data:getParams(orderby),
			method:"POST",
			success:function(data){
				$("#products").empty().html(data);
				$("#countTitle").empty().html($("#searchProductSize").val());
			}
		});
		
		$.ajax({
			url:"/"+Udwin.appName+"/product/pages",
			data:getParams(orderby),
			method:"POST",
			success:function(data){
				$(".ui-page").empty().html(data);
			}
		});
	})
	
	var jumpto = function(jumpPage){
		if($(".ui-page-cur")){
			$(".ui-page-cur").removeClass("ui-page-cur");
		}
		$(".ui-page-num a[page="+jumpPage+"]").addClass("ui-page-cur");
		$("#jumpto").val(jumpPage);
		
		var orderby = $(".sorted").attr("field");
		if($(".sorted b").attr("class").indexOf("asc")!=-1){
			orderby = orderby +" " + "asc";
		}else{
			orderby = orderby +" " + "desc";
		}
		console.log(orderby);
		var queryParams = getParams(orderby);
		queryParams.curPage = jumpPage;
		$.ajax({
			url:"/"+Udwin.appName+"/product/search",
			data:queryParams,
			method:"POST",
			success:function(data){
				$("#products").empty().html(data);
				$("#countTitle").empty().html($("#searchProductSize").val());
			}
		});
	}
	
	//上一页
	$(".ui-page").delegate(".ui-page-prev","click",function(){
		var jumpPage = parseInt($("#jumpto").val());
		if(jumpPage==1){
			return;
		}
		jumpPage = jumpPage-1;
		jumpto(jumpPage);
		
	});
	//下一页
	$(".ui-page").delegate(".ui-page-next","click",function(){
		var jumpPage = parseInt($("#jumpto").val());
		var pageCount = parseInt($("#pageCount").val());
		if(jumpPage==pageCount){
			return;
		}
		jumpPage = jumpPage+1;
		jumpto(jumpPage);
	})
	//enter page
	$(".ui-page").delegate(".ui-btn-s","click",function(){
		var pageCount = parseInt($("#pageCount").val());
		var jumpPage=parseInt($("#jumpto").val());
		if(jumpPage>pageCount || jumpPage < 1){
			return;
		}
		jumpto(jumpPage);
	})
	//select page
	$(".ui-page").delegate(".ui-page-num a[icon='1']","click",function(){
		var jumpPage = parseInt($(this).html());
		jumpto(jumpPage);
	})
	
	
	//详情页面
	$("#products").delegate(".productImg","click",function(){
		var productUuid = $(this).attr("value");
		window.location.href = "/"+Udwin.appName+"/product/detail/"+productUuid;
	})
	//详情页面
	$("#pro_right").delegate(".productImg","click",function(){
		var productUuid = $(this).attr("value");
		window.location.href = "/"+Udwin.appName+"/product/detail/"+productUuid;
	})
	
	//定制量尺页面
	$("#workflow").delegate("#dzlc","click",function(){
		window.open("/"+Udwin.appName+"/resources/html/dzlc.html");
	})
	
	/*$("body").delegate(".freeMeasure","click",function(){
		window.location.href="/"+Udwin.appName+"/resources/html/dzlchover.html";
	})*/
})